package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.ServiciosCliente;
import service.SistemaCVR;
import bean.vo.ClienteVO;
import bean.vo.RemitoClienteVO;

/**
 * Servlet implementation class SelecClienteFacturar
 */
public class SelecClienteFacturar extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SelecClienteFacturar() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		String cuit = request.getParameter("cuit");
		ClienteVO clienteTemp = new ClienteVO();
		clienteTemp.setCuit(cuit);
		try {
			ClienteVO cliente = ServiciosCliente.buscarCliente(clienteTemp);
			if(cliente == null){
				session.setAttribute("mensaje", "No se encontro un cliente con el cuit ingresado");
				RequestDispatcher dispatcher = request.getRequestDispatcher("/selecCliente.jsp");
				dispatcher.forward(request, response);
			}else{
				session.setAttribute("mensaje", "");
				session.setAttribute("cliente", cliente);
				
				List<RemitoClienteVO> remitosPendientes = SistemaCVR.getRemitosPendientes(cliente);
				session.setAttribute("remitos", remitosPendientes);
				
				RequestDispatcher dispatcher = request.getRequestDispatcher("/listaCondPagoRemito.jsp");
				dispatcher.forward(request, response);
			}
		
		} catch (Exception e) {
			session.setAttribute("mensaje", "Se produjo un error al buscar el cliente.");
		}finally{			
		}
	}

}
